package com.shanling.shanlingcontroller.ui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.linkplay.lpvr.avslib.LPAVSManager;
import com.linkplay.lpvr.avslib.LPAVSPlayer;
import com.linkplay.lpvr.lpvrbean.interfaces.templateruntime.LPAVSRenderPlayerInfo;
import com.shanling.shanlingcontroller.R;
import com.shanling.shanlingcontroller.base.BaseAppCompatActivity;
import com.shanling.shanlingcontroller.bean.AlexaPlayInfoEvent;
import com.shanling.shanlingcontroller.bean.BluetoothState;
import com.shanling.shanlingcontroller.bean.Constants;
import com.shanling.shanlingcontroller.bean.EventCenter;
import com.shanling.shanlingcontroller.utils.FormatUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

public class AlexaMusicActivity extends BaseAppCompatActivity {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_cover)
    ImageView ivCover;
    @BindView(R.id.tv_song_name)
    TextView tvSongName;
    @BindView(R.id.tv_singer)
    TextView tvSinger;
    @BindView(R.id.tv_startTime)
    TextView tvStartTime;
    @BindView(R.id.musicTimeSb)
    SeekBar musicTimeSb;
    @BindView(R.id.tv_endTime)
    TextView tvEndTime;
    @BindView(R.id.iv_play)
    ImageView ivPlay;
    @BindView(R.id.iv_last)
    ImageView ivLast;
    @BindView(R.id.lv_next)
    ImageView lvNext;
    @BindView(R.id.tv_Provider)
    TextView tvProvider;
    private Handler handler= new Handler() {
        public void handleMessage(Message msg) {
            //更新当前viewpager的 要显示的当前条目
            if(msg.what==MUSIC_WHAT){
                Log.e("weiyu","消息来了"+LPAVSManager.getInstance(mContext).getPlayer().getDuration());
                musicTimeSb.setProgress( LPAVSManager.getInstance(mContext).getPlayer().getCurrentTime());
                tvStartTime.setText(FormatUtil.INSTANCE.formatTime( LPAVSManager.getInstance(mContext).getPlayer().getCurrentTime()));
                if(  LPAVSManager.getInstance(mContext).getPlayer().getCurrentTime()== LPAVSManager.getInstance(mContext).getPlayer().getDuration()){
                    handler.removeCallbacksAndMessages(null);
                    return;
                }
                handler.sendEmptyMessageDelayed(MUSIC_WHAT, 1000);
            }
        }
    };;
    private  final  int MUSIC_WHAT=1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);

    }

    @Override
    protected void getBundleExtras(Bundle extras) {
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_alexa_music;
    }

    @Override
    protected void initViewsAndEvents() {
        musicTimeSb.setEnabled(false);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected boolean isBindEventBusHere() {
        return true;
    }

    @Override
    protected void onEventComing(EventCenter eventCenter) {
        switch (eventCenter.getEventCode()) {
            case Constants.BLUETOOTH_STATE:
                BluetoothState state = (BluetoothState) eventCenter.getData();
                if (state == null ) {
                    return;
                }
                if(state.getState()==Constants.DISCONNECTED){
                    finish();
                }
                break;
        }

    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return true;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return TransitionMode.BOTTOM;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        handler.removeCallbacksAndMessages(null);
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEvent(AlexaPlayInfoEvent eventCenter) {
        if (eventCenter != null) {
            LPAVSRenderPlayerInfo info = eventCenter.getPlayerInfo();
            if (info == null) {
                return;
            }
            initMusicData(info);
        }
    }

    private void initMusicData(LPAVSRenderPlayerInfo musicEntity) {
        LPAVSPlayer mLinkPlayAVSPlayer= LPAVSManager.getInstance(mContext).getPlayer();
        handler.removeCallbacksAndMessages(null);
        String provider = musicEntity.getProvider();
        tvProvider.setText(provider);
        tvSongName.setText(musicEntity.getTitle());
        tvSinger.setText(musicEntity.getSubTitle1());
        ivLast.setEnabled(musicEntity.isPreviousEnabled());
        lvNext.setEnabled(musicEntity.isNextEnabled());
        if (musicEntity.getArt() != null) {
            String url = musicEntity.getArt().getUrl();
            if (!TextUtils.isEmpty(url)) {
                Glide.with(mContext).load(url).placeholder(R.drawable.player_img_default).error(R.drawable.player_img_default).into(ivCover);
            }
        }
        if (mLinkPlayAVSPlayer.isPlaying()) {
            ivPlay.setImageResource(R.drawable.player_btn_pause);
        } else {
            ivPlay.setImageResource(R.drawable.player_btn_play);
        }
        musicTimeSb.setMax((int) mLinkPlayAVSPlayer.getDuration() );
        tvEndTime.setText(FormatUtil.INSTANCE.formatTime(mLinkPlayAVSPlayer.getDuration()));
        handler.sendEmptyMessageDelayed(MUSIC_WHAT,1000);

    }
    @OnClick({R.id.iv_back, R.id.iv_play, R.id.iv_last, R.id.lv_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_play:
                if(LPAVSManager.getInstance(mContext).getPlayer().isPlaying()){
                    LPAVSManager.getInstance(mContext).getPlayer().pause();
                    ivPlay.setImageResource(R.drawable.player_btn_play);
                }else {
                    LPAVSManager.getInstance(mContext).getPlayer().play();
                    ivPlay.setImageResource(R.drawable.player_btn_pause);
                }
                break;
            case R.id.iv_last:
                LPAVSManager.getInstance(mContext).getPlayer().previous();
                break;
            case R.id.lv_next:
                LPAVSManager.getInstance(mContext).getPlayer().next();
                break;
        }
    }
}
